// pages/goods/goods.js
const api = require('../../config/api')
const util = require('../../utils/util')

Page({
    data: {
        id: 0,
        goods: {}, //商品数据
        content: '', //商品富文本
        attribute: [], //商品参数
        issueList: [], //常见问题数据
        comment: [], //评论
        brand: {}, //制造商信息
        specificationList: [], //规格列表
        productList: [], //一个商品对应的不同产品
        checkedSpecPrice: 0, //商品价格
        checkedSpecPrice1: 0, //商品价格
        chooseText: '请选择规格数量',
        relateGoods: [], //相关商品
        openAttr: false,
        number: 1,
        NewchooseText: [], //sku显示的文字
        choseDone:false,
        productId:0,
        goodsCount:0//购物车数量
    },
    // onLoad(options)从地址中获取数据，当你从/pages/goods/goods?id={{item.id}}  获取的就是id
    onLoad(options) {
        // console.log(options);
        if (options) {
            this.setData({
                id: options.id
            })
        }
        this.getData()
    },
    // 点击购物车图标
    onClickIcon(){
        wx.switchTab({
            url:'/pages/cart/cart'
        })
    },
    // 加入购物车
    onClickButton(){
      let that=this
        util.request(api.CartAdd,{
          goodsId:that.data.id,
          number:that.data.number,
          productId:that.data.productId
        },'POST').then(res=>{
          // console.log(res);
          if (res.errno == 0) {
            wx.showToast({
              title: '添加成功'
            });
            this.getGoodsCount()
            that.setData({
              openAttr:false
            })
          }   
        })
    },
    // choose
    chooseI(e) {
        // console.log(e);
        let Name = e.currentTarget.dataset.name;
        let valueId = e.currentTarget.dataset.valueId;
        let newspecificationList = this.data.specificationList;
        // let NewchooseText1=[];
        for (let i = 0; i < newspecificationList.length; i++) {
            if (newspecificationList[i].name == Name) {
                for (let j = 0; j < newspecificationList[i].valueList.length; j++) {
                    if (newspecificationList[i].valueList[j].id == valueId) {
                        newspecificationList[i].valueList[j].checked = !newspecificationList[i].valueList[j].checked
                        //——————————————————————————————
                        if (newspecificationList[i].valueList[j].checked == true) {
                            this.data.NewchooseText[i] = newspecificationList[i].valueList[j].value
                        } else {
                            this.data.NewchooseText[i] = ''
                            // this.data.NewchooseText.splice(this.data.NewchooseText[i])
                        }
                    } else {
                        newspecificationList[i].valueList[j].checked = false
                    }
                }
            }
        }
        this.setData({
            specificationList: newspecificationList,
            NewchooseText: this.data.NewchooseText,
            chooseText: this.data.NewchooseText.toString()
        });
        // 获得价格
        this.getPrice()
        // 改变规格文字信息
        // this.changeInfo()
    },
    // 获得价格
    getPrice() {
        let newPrice = this.data.checkedSpecPrice1
        for (let i = 0; i < this.data.productList.length; i++) {
            if(this.data.productList[i].specifications.toString()==this.data.NewchooseText.toString()){
                newPrice=this.data.productList[i].price
                // console.log(newPrice);
                this.data.choseDone=true
                // ____________________________
                this.data.productId=this.data.productList[i].id
                break;
                // return
                // checkedSpecPrice
            }else{
                this.data.choseDone=false
            }
        }
        this.setData({
            checkedSpecPrice: newPrice,
            choseDone:this.data.choseDone,
            productId:this.data.productId
        })
    },
    // 改变规格文字信息
    // changeInfo(){
    //     let newspecificationList=this.data.specificationList;
    // },
    // 点击步进器
    onChange(e) {
        // console.log(e);
        this.setData({
            number: e.detail
        })
    },

    // 点击关闭sku
    closeSku() {
        this.setData({
            openAttr: !this.data.openAttr
        })
    },
    // 获取购物车商品数量
    getGoodsCount(){
        util.request(api.CartGoodsCount).then(res=>{
            // console.log(res);
            this.setData({
                goodsCount:res.data
            })
        })
    },
    // 点击规格选择
    switchAttrPop() {
        this.setData({
            openAttr: !this.data.openAttr
        })
    },
    getData() {
        let that = this
        util.request(api.GoodsDetail, {
            id: that.data.id
        }).then(res => {
            // console.log(res);
            if (res.errno == 0) {
                that.setData({
                    goods: res.data.info,
                    // content: res.data.info.detail.replace(/\<img/gi, '<img class="diy-img"'),
                    attribute: res.data.attribute,
                    issueList: res.data.issue,
                    comment: res.data.comment,
                    brand: res.data.brand,
                    specificationList: res.data.specificationList,
                    productList: res.data.productList,
                    checkedSpecPrice: res.data.info.retailPrice,
                    checkedSpecPrice1: res.data.info.retailPrice
                });
                that.getGoodsRelated()
                that.getGoodsCount()
            }
        })
    },
    // 获取相关商品数据
    getGoodsRelated() {
        let that = this;
        util.request(api.GoodsRelated, {
            id: that.data.id
        }).then(res => {
            if (res.errno == 0) {
                that.setData({
                    relateGoods: res.data.goodsList
                })
            }
        })
    }
})